<?php
/** .-------------------------------------------------------------------
* |    Author: 熊伟洋 <chelious@foxmail.com>
* |    WeChat: hello_McGrady
* |    	   QQ: 434493420
* |     Motto: Hungry & Humble
* |---------------------------------------------------------------------
* |    Copyright (c) 2012-2020, www.chelious.com. All Rights Reserved.
* '-------------------------------------------------------------------*/

namespace app\admin\controller;

class Link extends Common {
    private $model;

    public function __construct()
    {
        $this->model = new \system\model\Link();
        parent::__construct();
    }

    //加载模版
    public function index(){
        //分页
        $data = Db::table('link')->paginate(8);
        $page = $data->links();
        $data = $data->toArray();
//        p($data);
        //分配变量
        View::with(['data' => $data , 'page' => $page]);
        //加载模版
        return view();
    }

    //添加链接
    public function add(){
        if(IS_POST){
            if($this->model->add()){
                message('链接添加成功',u('index'),'success');
            }else{
                message($this->model->getError(),'back','error');
            }
        }

        return view();
    }

    //修改链接
    public function edit(){
        $lid = Q('get.lid');
//        p($lid);
        $linkData = Db::table('link')->where('lid',$lid)->first();
        View::with('linkData',$linkData);
//        p($linkData);
        if(IS_POST){
            if($this->model->edit()){
                message('链接修改成功',u('index'),'success');
            }else{
                message($this->model->getError(),'back','error');
            }
        }

        return view();
    }

    //删除链接
    public function del(){
        $lid = Q('get.lid');
//        p($lid);
        $this->model->where('lid',$lid)->delete();
        message('链接删除成功',u('index'),'success');
    }

}
